/*
 * EcmaScriptException.java - ScriptME
 * 
 * Copyright (c) 2009 Cesar Henriques <cesar at alttab.com.ar>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Based on FESI Project
 * 
 * Contributors:
 * 	Jean-Marc Lugrin - initial API and implementation
 * 	Cesar Henriques <cesar at alttab.com.ar> - J2ME Porting and Extensions
 */
package org.scriptme.ecmascript.exceptions;

import java.io.PrintStream;
import java.util.Vector;

import org.scriptme.ecmascript.interpreter.EvaluationSource;

/**
 * Superclass of all common exceptions used by the FESI system.
 */
public class EcmaScriptException extends Exception {

	/** The end of line string for this machine. */
	static protected String eol = System.getProperty("line.separator") == null ? "\n"
			: System.getProperty("line.separator");

	/** The list of EcmaScript evaluation sources at this error. */
	protected Vector evaluationSources = new Vector();

	/** The originating exception. */
	private Throwable originatingException = null; // If the exception package
													// another one

	/**
	 * Create a generic exception with cause "reason".
	 * 
	 * @param reason
	 *            the reason
	 */
	public EcmaScriptException(String reason) {
		super(reason);
	}

	/**
	 * Create a generic exception with cause "reason", originially caused by the
	 * originatingException.
	 * 
	 * @param reason
	 *            the reason
	 * @param originatingException
	 *            the originating exception
	 */
	public EcmaScriptException(String reason, Throwable originatingException) {
		super(reason);
		this.originatingException = originatingException;
	}

	/**
	 * Get the originating exception (if any) or null.
	 * 
	 * @return originating exception or null.
	 */
	public Throwable getOriginatingException() {
		return originatingException;
	}

	/**
	 * Append an evaluation source to the list of evaluation sources.
	 * 
	 * @param es
	 *            The evaluation source to add
	 */
	public void appendEvaluationSource(EvaluationSource es) {
		evaluationSources.addElement(es);
	}

	/**
	 * Get the line number of the error if possible.
	 * 
	 * @return the line number
	 */
	public int getLineNumber() {

		if (evaluationSources.size() > 0) {
			EvaluationSource es = (EvaluationSource) evaluationSources
					.elementAt(0);
			return es.getLineNumber();
		} else {
			return -1;
		}
	}

	/**
	 * Display the message, the originating exception and the EmcaScript
	 * callback chain.
	 * <P>
	 * If cause by another exception displays its callback chain
	 * 
	 * @return the message
	 */
	public String getMessage() {
		String msg = "Runtime error " + super.getMessage();
		if (originatingException != null) {
			msg += eol + "Caused by exception: " + eol + "  "
					+ originatingException.getMessage();
		}
		if (originatingException != null) {
			originatingException.printStackTrace();
			msg += eol;
		}
		for (int i = 0; i < evaluationSources.size(); i++) {
			EvaluationSource es = (EvaluationSource) evaluationSources
					.elementAt(i);
			msg += eol + (i == 0 ? "detected " : "called ") + es;
		}
		return msg;
	}

	/**
	 * If true would the the parser error detected when a statement is
	 * incomplete.
	 * 
	 * @return true, if checks if is incomplete
	 */
	public boolean isIncomplete() {
		return false;
	}

	/**
	 * Prints this <code>Throwable</code> and its backtrace to the standard
	 * error stream.
	 */
	public void printStackTrace() {
		System.err.println(this);
		printStackTrace0(System.err);
	}

	/**
	 * Prints this <code>Throwable</code> and its backtrace to the specified
	 * print stream.
	 * 
	 * @param s
	 *            the s
	 */
	public void printStackTrace(PrintStream s) {
		s.println(this);
		printStackTrace0(s);
	}

	/**
	 * Print the stack trace to a stream, with the backtrace of the originating
	 * exception if any.
	 * 
	 * @param w
	 *            the w
	 */
	private void printStackTrace0(PrintStream w) {
		super.printStackTrace();

		if (originatingException != null) {
			w.println("due to:");
			originatingException.printStackTrace();
		}
		w.flush();
	}
}
